package com.heima.wemedia.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.minio.MinIOTemplate;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dto.WmMaterialDto;
import com.heima.model.wemedia.entity.WmMaterial;
import com.heima.wemedia.dao.WmMaterialDao;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.utils.WmThreadLocalUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * 自媒体素材表(WmMaterial)表服务实现类
 *
 * @author makejava
 * @since 2022-04-20 22:04:29
 */
@Service("wmMaterialService")
public class WmMaterialServiceImpl implements WmMaterialService {
    @Resource
    private WmMaterialDao wmMaterialDao;
    @Resource
    private MinIOTemplate minIOTemplate;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public WmMaterial queryById(Long id) {
        return this.wmMaterialDao.queryById(id);
    }


    /**
     * 新增数据
     *
     * @param wmMaterial 实例对象
     * @return 实例对象
     */
    @Override
    public WmMaterial insert(WmMaterial wmMaterial) {
        this.wmMaterialDao.insert(wmMaterial);
        return wmMaterial;
    }

    /**
     * 修改数据
     *
     * @param wmMaterial 实例对象
     * @return 实例对象
     */
    @Override
    public WmMaterial update(WmMaterial wmMaterial) {
        this.wmMaterialDao.update(wmMaterial);
        return this.queryById(Long.valueOf(wmMaterial.getId()));
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.wmMaterialDao.deleteById(id) > 0;
    }


    /**
     * 素材列表查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult queryByPage(WmMaterialDto dto) {
        //校验参数
        dto.checkParam();

        //开启分页
        PageHelper.startPage(dto.getPage(), dto.getSize());
        //执行查询
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setId((WmThreadLocalUtil.get().getId()));
        if (dto.getIsCollection() == 1) {
            wmMaterial.setIsCollection(String.valueOf(dto.getIsCollection()));
        }

        //查询数据库
        Page<WmMaterial> page = wmMaterialDao.materialPage(wmMaterial);

        //封装数据返回
        ResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        result.setData(page.getResult());
        return result;
    }

    /**
     * 自媒体素材上传
     *
     * @param
     * @return
     */
    @Override
    public ResponseResult upload(MultipartFile multipartFile) throws IOException {
        if (multipartFile == null || multipartFile.getSize() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //2.上传图片到minio
        String originalFilename = multipartFile.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileName = UUID.randomUUID().toString().replace("-", "") + suffix;

        String url = minIOTemplate.uploadImgFile("", fileName, multipartFile.getInputStream());

        WmMaterial wmMaterial = new WmMaterial();

        wmMaterial.setUserId(Long.valueOf(WmThreadLocalUtil.get().getId()));
        wmMaterial.setUrl(url);
        wmMaterial.setType(0);
        wmMaterial.setIsCollection("");
        wmMaterial.setCreatedBy(0);
        wmMaterial.setUpdatedBy(0);
        wmMaterial.setCreatedTime(new Date());
        wmMaterial.setUpdatedTime(new Date());

        wmMaterialDao.insert(wmMaterial);
        return ResponseResult.okResult(wmMaterial);
    }
}
